package com.lzm.ydpt.module.order.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.base.TabFragmentAdapter;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonutils.DisplayUtil;
import com.byt.framlib.commonwidget.ClearableEditText;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.flycotab.SlidingTabLayout;
import com.byt.framlib.commonwidget.flycotab.listener.OnTabSelectListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.module.order.fragment.AddressBookFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 地址簿
 */
public class AddressBookActivity extends BaseActivity {

    @BindView(R.id.ntb_address_book)
    NormalTitleBar ntb_address_book;
    @BindView(R.id.ed_common_search_content)
    ClearableEditText ed_common_search_content;
    @BindView(R.id.tab_address_book)
    SlidingTabLayout tab_address_book;
    @BindView(R.id.vp_address_book)
    ViewPager vp_address_book;

    private int currentPosition = 0;
    private List<String> detailList = new ArrayList<>();
    private ArrayList<BaseFragment> fragments = new ArrayList<>();

    /**
     * 0 == 直接进来
     * 1 == 选择地址进来
     */
    private int jumpType = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_address_book;
    }

    @Override
    public BasePresenter initPreData() {
        return null;
    }

    @Override
    public void initView() {
        jumpType = getIntent().getIntExtra(Constants.INP_ADDRESS_MANAGER_TYPE, 0);
        setNtbTitle(ntb_address_book, false);
        initTitle();
        initTab();
        ed_common_search_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                // 按下按钮，这里和xml文件中的EditText中属性imeOptions对应;
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // setResult(ed_common_search_content.getText().toString());
                    return false;
                }
                return true;//返回true，保留软键盘;false，隐藏软键盘
            }
        });
    }

    private void initTab() {
        fragments.clear();
        detailList.clear();
        detailList.add("发货地址");
        detailList.add("送货地址");
        fragments.add(AddressBookFragment.newsInstance(1, jumpType));
        fragments.add(AddressBookFragment.newsInstance(2, jumpType));
        vp_address_book.setAdapter(new TabFragmentAdapter(getSupportFragmentManager(), fragments, detailList));
        vp_address_book.setOffscreenPageLimit(fragments.size());
        tab_address_book.setTabWidthPx(fragments.size() > 4 ? DisplayUtil.getScreenWidth(mBContext) / 4 : DisplayUtil.getScreenWidth(mBContext) / fragments.size());
        tab_address_book.setViewPager(vp_address_book);
        tab_address_book.setCurrentTab(currentPosition);
        tab_address_book.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                currentPosition = position;
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void initTitle() {
        ntb_address_book.setTitleText("地址薄");
        ntb_address_book.setOnBackListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.tv_address_book})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_address_book:
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.INP_APPEND_ADDRESS_TYPE, currentPosition == 0 ? 1 : 2); // 列表添加
                openActivity(AddAddressBookActivity.class, bundle);
                break;
        }
    }
}